<template>
  <a-upload
    v-model:file-list="fileData"
    name="resume"
    accept=".doc,.docx,.pdf"
    :show-upload-list="false"
    :customRequest="customRequest()"
    :before-upload="onBeforeUpload"
    :maxCount="1"
  >
    <slot></slot>
  </a-upload>
</template>

<script lang="ts" setup>
import ResumeService from '@/api/resume';
import { _setInterval, fileLt20M } from '@/utils/util';
import { message } from 'ant-design-vue';
import { onUnmounted, ref } from 'vue';

const fileData = ref();
const emit = defineEmits(['success', 'error']);
let interval: any;
function onBeforeUpload(file: any) {
  const isLt = fileLt20M(file);
  if (!isLt) {
    message.error('简历超过20M，请压缩后重新上传');
  }
  return isLt;
}

// function intervalLoop(id: string) {
//   const { callback } = _setInterval(loop, 1000);
//   async function loop() {
//     const resp = await ResumeService.selectAnalsisResumeFromSeekerLogById({
//       mongoId: id,
//     });

//     if (resp.data?.status === 'success') {
//       callback();
//       message.success('上传成功');
//       emit('success');
//     } else if (resp.data?.status === 'fail') {
//       message.error(resp.data?.message || '操作失败');
//       callback();
//       emit('error');
//     }
//   }
// }

function customRequest() {
  return async function ({ onSuccess, onError, file }: any) {
    try {
      const formData = new FormData();
      formData.append('file', file);
      const resp = await ResumeService.uploadResume(formData);
      if (resp.code === 200) {
        // intervalLoop(resp.data);
        message.success('上传成功');
        emit('success', resp.data);
      } else {
        onError(file);
      }
    } catch (err) {
      onError(file);
    }
  };
}

onUnmounted(() => {
  clearInterval(interval);
});
</script>
<style lang="less" scoped></style>
